<template>
  <el-dialog
    :before-close="close"
    :width="dialogWidth"
    :title="title"
    :visible.sync="isShow"
    v-if="isShow"
    center
    v-dialogDrag
  >
    <el-form :model="form" ref="form" :rules="rules" class="mt20">
      <el-col :span="8" class="mb20">
        <el-form-item label="渠道：" prop="promotion" :label-width="formLabelWidth">
          <el-select placeholder="请选择渠道" v-model="form.promotion">
            <el-option v-for="item in promotionList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="时间：" prop="time" :label-width="formLabelWidth">
          <div class="block">
            <el-date-picker
              v-model="form.time"
              align="right"
              type="date"
              placeholder="选择日期"
              :picker-options="pickerOptions1"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </div>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="客户电话：" prop="phone" label-width="85px">
          <el-input v-model="form.phone" autocomplete="off" placeholder="请输入客户电话"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="中心：" prop="center" :label-width="formLabelWidth">
          <el-select placeholder="请选择中心名" v-model="form.center" @change="getDeptList">
            <el-option v-for="item in centerList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="部门：" prop="dept" :label-width="formLabelWidth">
          <el-select placeholder="请选择部门名" v-model="form.dept" @change="getSalesmanList">
            <el-option v-for="item in deptList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="业务员：" prop="salesman" label-width="90px">
          <el-select placeholder="请选择业务员名" v-model="form.salesman">
            <el-option v-for="item in salesmanList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="区域：" prop="distribution" :label-width="formLabelWidth">
          <el-select placeholder="请选择区域" v-model="form.distribution">
            <el-option v-for="item in distributionList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="姓名：" prop="name" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" placeholder="请输入客户姓名"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="购车类型：" prop="is_mortgage" label-width="95px">
          <el-radio-group v-model="form.is_mortgage">
            <el-radio border label="按揭"></el-radio>
            <el-radio border label="全款"></el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="谈判师：" prop="negotiator" label-width="80px">
          <el-select placeholder="请选择谈判师" v-model="form.negotiator">
            <el-option label="无" value="无"></el-option>
            <el-option v-for="item in negotiatorList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="是否签订：" prop="is_sign" label-width="100px">
          <el-radio-group v-model="form.is_sign" @change="handleIsSign">
            <el-radio label="已签订"></el-radio>
            <el-radio label="未签订"></el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <div v-if="flag||editDialog">
        <el-col :span="8" v-if="isSign">
          <el-form-item label="定金(元)：" prop="deposit" label-width="90px">
            <el-input
              v-model="form.deposit"
              autocomplete="off"
              type="number"
              placeholder="请输入定金(元)"
              @keyup.enter.native="handleConfirm('form')"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" v-else>
          <el-form-item label="未签订原因：" prop="notsigning" label-width="100px">
            <el-select placeholder="请选择未签订原因" v-model="form.notsigning">
              <el-option key="无" label="无" value="无" @keyup.enter.native="handleConfirm('form')"></el-option>
              <el-option
                v-for="item in notsigningList"
                :key="item"
                :label="item"
                :value="item"
                @keyup.enter.native="handleConfirm('form')"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="isSign">
          <el-form-item label="定金转业绩：" prop="depositToAchievement" label-width="120px">
            <el-switch v-model="form.depositToAchievement"></el-switch>
          </el-form-item>
        </el-col>
      </div>
    </el-form>
    <span slot="footer" class="dialog-footer align-center tiling inline-block mt20 mb20">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="handleConfirm('form')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import "../utils/drag.js";
import { pickerOptions1 } from "@/../config/Stats_options";

export default {
  name: "record_dialog",
  data() {
    var validatePhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入客户电话！"));
      } else if (value.toString().search(/^(13|14|15|17|18|19)\d{9}$/) === -1) {
        return callback(new Error("请输入11位电话号码！"));
      } else {
        callback();
      }
    };
    var validateName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入客户姓名！"));
      } else if (value.search(/^[\u0391-\uFFE5]+$/) === -1) {
        return callback(new Error("请输入中文！"));
      } else if (value.length < 2) {
        return callback(new Error("长度在 2-6 个汉字"));
      } else if (value.length > 6) {
        return callback(new Error("长度在 2-6 个汉字"));
      } else {
        callback();
      }
    };
    var validateDeposit = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入定金！"));
      } else if (value.toString().search(/^[0-9]*$/) === -1) {
        return callback(new Error("请输入数字！"));
      } else if (value.length > 5) {
        return callback(new Error("长度在 1-5 个数字"));
      } else {
        callback();
      }
    };
    var validateNotsigning = (rule, value, callback) => {
      if (!value && !this.isSign) {
        return callback(new Error("请选择未签订原因！"));
      } else {
        callback();
      }
    };
    return {
      form: {
        time: "",
        promotion: "",
        deposit: "",
        name: "",
        phone: "",
        center: "",
        is_sign: "",
        dept: "",
        salesman: "",
        notsigning: "",
        distribution: "",
        is_mortgage: "",
        negotiator: "",
        depositToAchievement: false
      },
      rules: {
        time: { required: true, message: "请选择分配时间！", trigger: "blur" },
        promotion: {
          required: true,
          message: "请选择推广渠道！",
          trigger: "blur"
        },
        is_sign: {
          required: true,
          message: "请选择签订情况！",
          trigger: "blur"
        },
        is_mortgage: {
          required: true,
          message: "请选择购车类型！",
          trigger: "blur"
        },
        notsigning: { validator: validateNotsigning, trigger: "blur" },
        deposit: { validator: validateDeposit, trigger: "blur" },
        name: { validator: validateName, trigger: "blur" },
        phone: { validator: validatePhone, trigger: "blur" },
        negotiator: {
          required: true,
          message: "请选择谈判师！",
          trigger: "blur"
        },
        center: {
          required: true,
          message: "请选择分配中心！",
          trigger: "blur"
        },
        dept: { required: true, message: "请选择分配部门！", trigger: "blur" },
        distribution: {
          required: true,
          message: "请选择客户分布区域！",
          trigger: "blur"
        },
        salesman: {
          required: true,
          message: "请选择分配业务员！",
          trigger: "blur"
        }
      },
      flag: false,
      centerList: [],
      deptList: [],
      salesmanList: [],
      negotiatorList: [],
      promotionList: [],
      notsigningList: [],
      distributionList: [],
      pickerOptions1,
      formLabelWidth: "70px",
      dialogWidth: "1000px"
    };
  },
  computed: {
    isShow() {
      return this.addDialog || this.editDialog;
    },
    isSign() {
      return this.form.is_sign === "已签订" ? true : false;
    }
  },
  created() {
    this.getCenterList();
    this.getPromotionList();
    this.getNotsigningList();
    this.getDistributionList();
    this.getNegotiatorList();
  },
  watch: {
    editDialog() {
      this.form = {
        time: this.recordForm.time,
        promotion: this.recordForm.promotionName,
        deposit: this.recordForm.deposit,
        name: this.recordForm.name,
        phone: this.recordForm.phone,
        is_sign: this.recordForm.isSign,
        center: this.recordForm.centerName,
        dept: this.recordForm.deptName,
        salesman: this.recordForm.salesmanName,
        notsigning: this.recordForm.notsign,
        distribution: this.recordForm.distributionName,
        is_mortgage: this.recordForm.isMortgage,
        negotiator: this.recordForm.negotiator,
        depositToAchievement: this.recordForm.depositToAchievement
      };
    }
  },
  methods: {
    handleIsSign() {
      this.flag = true;
      this.form.is_sign === "已签订"
        ? (this.form.notsigning = "无")
        : ((this.form.deposit = 0), (this.form.depositToAchievement = false));
    },
    getDeptList() {
      this.form.dept = "";
      this.form.salesman = "";
      this.$axios.post("/api/system/departmentList", this.form).then(data => {
        this.deptList = data.map(item => {
          return item.name;
        });
      });
    },
    getSalesmanList() {
      this.form.salesman = "";
      this.$axios.post("/api/system/salesmanList", this.form).then(data => {
        this.salesmanList = data.map(item => {
          return item.name;
        });
      });
    },
    getPromotionList() {
      this.$axios.get("/api/system/promotions").then(data => {
        this.promotionList = data.map(item => {
          return item.name;
        });
      });
    },
    getCenterList() {
      this.$axios.get("/api/system/centers").then(data => {
        this.centerList = data.map(item => {
          return item.name;
        });
      });
    },
    getNegotiatorList() {
      this.$axios.get("/api/system/negotiators").then(data => {
        this.negotiatorList = data.map(item => {
          return item.name;
        });
      });
    },
    getNotsigningList() {
      this.$axios.get("/api/system/notsignings").then(data => {
        this.notsigningList = data.map(item => {
          return item.name;
        });
      });
    },
    getDistributionList() {
      this.$axios.get("/api/system/distributions").then(data => {
        this.distributionList = data.map(item => {
          return item.name;
        });
      });
    },
    handleConfirm(formName) {
      // 先进行表单验证
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.editDialog ? this.editRecord() : this.addRecord();
        }
      });
    },
    addRecord() {
      this.$axios.post("/api/business/addRecord", this.form).then(data => {
        if (data.status) {
          this.$message({
            type: "success",
            message: "添加成功！"
          });
          this.$emit("update");
        } else {
          this.$message({
            type: "error",
            message: data.msg
          });
        }
      });
    },
    editRecord() {
      this.$axios
        .put("/api/business/editRecord/" + this.recordForm.id, this.form)
        .then(data => {
          if (data.status) {
            this.$message({
              type: "success",
              message: "编辑成功！"
            });
            this.$emit("update");
            this.close();
          } else {
            this.$message({
              type: "error",
              message: data.msg
            });
          }
        });
    },
    close() {
      this.$emit("close");
      this.resetForm();
    },
    resetForm() {
      this.form = {
        time: "",
        promotion: "",
        deposit: "",
        phone: "",
        name: "",
        center: "",
        is_sign: "",
        dept: "",
        salesman: "",
        notsigning: "",
        distribution: "",
        is_mortgage: "",
        negotiator: "",
        depositToAchievement: false
      };
      this.flag = false;
    }
  },
  props: ["addDialog", "editDialog", "title", "recordForm"]
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
